﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseWebAPP.code.DAL;
using WarehouseWebAPP.code.DAL.LendDSTableAdapters;

namespace WarehouseWebAPP.code.BLL
{
    public class LendBLL
    {
        private lendTableAdapter LendAdapter = null;
        public lendTableAdapter Adapter
        {
            get
            {
                if (LendAdapter == null) LendAdapter = new lendTableAdapter();
                return LendAdapter;
            }
        }
        public void LendInsert(Guid lend_user, Guid lend_customer, Guid lend_item_num, DateTime lend_start_date, DateTime lend_end_date, DateTime lend_returned_date)
        {
            if (lend_user==null)
                throw new Exception ("Lending User is missing");
            if (lend_customer== null)
                throw new Exception ("Lend Customer is missing");
            if (lend_item_num == null)
                throw new Exception("Lend item Number is missing");
            if (lend_start_date == null)
                throw new Exception("Lend start date is missing");
            if (lend_end_date == null)
                throw new Exception("Lend end date is missing");

            //set the Availablity of this item to 0;
            LendItemBLL itemBLL = new LendItemBLL(); 
            itemBLL.LendItemSetUnavailable(lend_item_num);
            DateTime returndate = new DateTime(1753, 1, 1);//1753
            if (Adapter.LendInsert(lend_user, lend_customer, lend_item_num, lend_start_date, lend_end_date, returndate) < 1)
                throw new Exception ("Lending Item was not Submitted");

            UserBLL userBLL = new UserBLL();
            CustomerBLL customerBLL = new CustomerBLL();
            string item = itemBLL.LendItemByNum(lend_item_num)[0].lend_item_serial;
            string customer = customerBLL.CustomerGetByNum(lend_customer)[0].customer_name;
            string user = userBLL.userGetByNum(lend_user)[0].user_name;



            string[] Parameters = { item, customer, user, lend_end_date.ToLongDateString()};
            MyEvent ev = History.EventDecorator(History.ActionType.NewLend, Parameters, DateTime.Now);
            History.AddEvent(ev);
        }

        public void LendDelete(Guid lend_num)
        {
            if (lend_num == null)
                throw new Exception("Lending Number is missing");
            if (Adapter.LendDelete(lend_num) < 1)
                throw new Exception("Lending was not deleted");
        }

        public void LendUpdate(Guid lend_user, Guid lend_customer, Guid lend_item_num, DateTime lend_start_date, DateTime lend_end_date, DateTime lend_returned_date, Guid lend_num)
        {

            if (lend_num == null)
                throw new Exception("Lend Num is missing");
            if (lend_user == null)
                throw new Exception("Lending User is missing");
            if (lend_customer == null)
                throw new Exception("Lend Customer is missing");
            if (lend_item_num == null)
                throw new Exception("Lend item Number is missing");
            if (lend_start_date == null)
                throw new Exception("Lend start date is missing");
            if (lend_end_date == null)
                throw new Exception("Lend end date is missing");  
            if (Adapter.LendUpdate(lend_user, lend_customer, lend_item_num, lend_start_date, lend_end_date, lend_returned_date, lend_num) < 1)
                throw new Exception("Lending Item was not Submitted");

        }

        public LendDS.lendDataTable LendGetByItem (Guid item_num)
        {
            if (item_num==null)
                throw new Exception ("Item Number is missing");
            return Adapter.LendGetByItem(item_num);
        }

        public LendDS.lendDataTable LendGetAlerts()
        {
            DateTime oldest= new DateTime(1753,1,1);//unreturned lends
            return Adapter.LendGetAlert(oldest, DateTime.Now);
        }

        public LendDS.lendDataTable LendGetByNum(Guid lend_num)
        {
            if (lend_num == null)
                throw new Exception("Lend Number is missing");
            return Adapter.LendGetByNum(lend_num);
        }

        public LendDS.lendDataTable LendSearch(string lend_item_num, string lend_user, string lend_customer, DateTime lend_start_date_from, DateTime lend_start_date_to, 
            DateTime lend_end_date_from, DateTime lend_end_date_to, DateTime lend_returned_date_from, DateTime lend_returned_date_to)
        {
            return Adapter.LendSearch(lend_customer, lend_user, lend_item_num, lend_start_date_from, lend_start_date_to, 
             lend_end_date_from,  lend_end_date_to,  lend_returned_date_from,  lend_returned_date_to);

        }

        public LendDS.lendDataTable LendGetByCustomer (Guid customer_num)
        {
            if (customer_num==null)
                throw new Exception ("Customer Number is missing");
            return Adapter.LendGetByCustomer(customer_num);
        }

        public LendDS.lendDataTable LendGetByUser(Guid user_num)
        {
            if (user_num == null)
                throw new Exception("User Number is missing");
            return Adapter.LendGetByUser(user_num);
        }

        public LendDS.lendDataTable LendEndAfterGivenDate(DateTime lend_end_date)
        {
            if (lend_end_date == null)
                throw new Exception("Lend End Date is missing");
            return Adapter.LendEndAfterGivenDate(lend_end_date);
        }

        public LendDS.lendDataTable LendEndBeforeGivenDate(DateTime lend_end_date)
        {
            if (lend_end_date == null)
                throw new Exception("Lend End Date is missing");
            return Adapter.LendEndBeforeGivenDate(lend_end_date);
        }
        
        public LendDS.lendDataTable LendStartAfterGivenDate(DateTime lend_start_date)
        {
            if (lend_start_date == null)
                throw new Exception("Lend Start Date is missing");
            return Adapter.LendStartAfterGivenDate(lend_start_date);
        }

        public LendDS.lendDataTable LendStartAfteToday()
        {
            
            return Adapter.LendStartAfterGivenDate(DateTime.Today);
        }

        public LendDS.lendDataTable LendStartBeforeGivenDate(DateTime lend_start_date)
        {
            if (lend_start_date == null)
                throw new Exception("Lend Start Date is missing");
            return Adapter.LendStartBeforeGivenDate(lend_start_date);
        }

        public LendDS.lendDataTable LendReturnedAfterGivenDate(DateTime lend_returned_date)
        {
            if (lend_returned_date == null)
                throw new Exception("Lend Returned Date is missing");
            return Adapter.LendReturnedAfterGivenDate(lend_returned_date);
        }

        public LendDS.lendDataTable LendReturnedBeforeGivenDate(DateTime lend_returned_date)
        {
            if (lend_returned_date == null)
                throw new Exception("Lend Returned Date is missing");
            return Adapter.LendReturnedBeforeGivenDate(lend_returned_date);
        }

        public LendDS.lendDataTable LendGetByStartDates(DateTime lend_start_date_from, DateTime lend_start_date_to)
        {

            return Adapter.LendGetByStartDates(lend_start_date_from, lend_start_date_to);
        }
    }


}
